
const path=require('path')
const fs=require('fs')

const scrReg=/<script>[\s\S]*<\/script>/
const cssReg=/<style>[\s\S]*<\/style>/

fs.readFile(path.join(__dirname,'./index.html'),'utf8',function(err,data){
    if(!err){
        resolveCss(data)
        resolveScr(data)
        resolveHtml(data)
    }
})
function resolveCss(htmlstr){
    let csstr=cssReg.exec(htmlstr)
    let cssresult=csstr[0].replace('<style>','').replace('</style>','')
    fs.writeFile(path.join(__dirname,'../usehttp/clock/clock.css'),cssresult,'utf8',function(err){
        if(!err) console.log('css提取成功')
    })
}

function resolveScr(htmlstr){
    let scrtr=scrReg.exec(htmlstr)
    let scresult=scrtr[0].replace('<script>','').replace('</script>','')
    fs.writeFile(path.join(__dirname,'../usehttp/clock/clock.js'),scresult,'utf8',function(err){
        if(!err) console.log('css提取成功')
    })
}

function resolveHtml(htmlstr){
    const str=htmlstr.replace(cssReg,'<link rel="stylesheet" href="./clock.css">').replace(scrReg,'<script src="./clock.js"></script>')
    fs.writeFile(path.join(__dirname,'../usehttp/clock/clock.html'),str,'utf8',function(error){
        if(!error)console.log('html嵌入成功')
    })
}
